/*
 * Copyright 2020-2030 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitee.minimalismstyle.fresh.common.core.domain

import io.swagger.annotations.ApiModelProperty
import java.io.Serializable

/**
 *  通用响应对象
 * @author maoxiaodong
 */
class R<T>(

        /**
         * 是否成功
         */
        @ApiModelProperty(value = "是否成功")
        var success: Boolean = true,

        /**
         * 响应代码
         */
        @ApiModelProperty(value = "响应代码")
        var code: Int = 200,

        /**
         * 响应消息
         */
        @ApiModelProperty(value = "响应消息")
        val message: String? = null,

        /**
         * 响应数据
         */
        @ApiModelProperty(value = "响应数据")
        val data: T? = null) : Serializable {

    companion object {
        /**
         * 简单的成功
         * @return
         */
        fun <T> success(): R<T> {
            return R.success(null)
        }

        /**
         * 简单的成功
         * @param message 信息
         * @return
         */
        fun <T> success(message: String): R<T> {
            return R(message = message)
        }

        /**
         * 简单的成功
         * @param data 主体数据
         * @return
         */
        fun <T> success(data: T?): R<T> {
            return R(data = data)
        }

        /**
         * 简单的成功
         * @param data 主体数据
         * @return
         */
        fun <T> success(message: String, data: T?): R<T> {
            return R(message = message, data = data)
        }

        /**
         * 简单的错误
         * @param message 错误信息
         * @return
         */
        fun <T> error(message: String?): R<T> {
            return R(success = false, code = 500, message = message)
        }

        /**
         * 简单的错误
         * @param code 错误代码
         * @param message 错误信息
         * @return
         */
        fun <T> error(code: Int, message: String): R<T> {
            return R(success = false, code = code, message = message)
        }

        /**
         * 简单的错误
         * @param message 错误信息
         * @param data 主体数据
         * @return
         */
        fun <T> error(message: String, data: T?): R<T> {
            return R(success = false, code = 500, message = message, data=data)
        }

        /**
         * 简单的错误
         * @param message 错误信息
         * @param data 主体数据
         * @return
         */
        fun <T> error(code: Int, message: String, data: T?): R<T> {
            return R(success = false, code = code, message = message, data=data)
        }
    }

    /**
     * 成功回调
     * @param predicate 回调内容
     */
    fun success(predicate: (T?) -> Unit) {
        if(this.success){
            predicate(data)
        }
    }

    /**
     * 错误回调
     * @param predicate 回调内容
     */
    fun error(predicate: (R<T>) -> Unit): R<T> {
        if(!this.success){
            predicate(this)
        }
        return this
    }

    /**
     * 获取不为空的数据
     */
    fun get(): T {
        return data!!
    }

    /**
     * 获取不为空的数据
     * @param data 数据为空时的替代数据
     */
    fun get(data: T): T {
        return this.data ?: data
    }
}
